package com.example.util;

import java.util.ArrayList;
import java.util.List;

import android.view.View;
import android.view.ViewGroup;

import com.example.entity.ViewHolder;

public class ViewUtil {

	public static int getChildIndex(ViewGroup parent, View target) {
		for (int i = 0; i < parent.getChildCount(); i++) {
			View child = parent.getChildAt(i);
			if (child == target)
				return i;
		}
		
		return -1;
	}
	
	public static List<View> findViewsByStatus(ViewGroup root, boolean isSelected) {
		List<View> allViews = new ArrayList<View>();

		final int childCount = root.getChildCount();
		for (int i = 0; i < childCount; i++) {
			final View childView = root.getChildAt(i);
			final ViewHolder viewHolder = (childView.getTag() == null) ? null : (ViewHolder) childView.getTag();
			
			if (viewHolder != null && viewHolder.selected == isSelected)
				allViews.add(childView);

			if (childView instanceof ViewGroup)
				allViews.addAll(findViewsByStatus((ViewGroup) childView, isSelected));
		}

		return allViews;
	}
}
